/*
 遇到的问题:
 在setFrame方法里不可以添加控件
 */
#import "MainViewController.h"
#import "XBCommunityScrollView.h"
#import "XBCommunityLabel.h"
#import "Header.h"

@interface XBCommunityScrollView ()

/** 频道文本下的线 */
//@property (nonatomic,weak) UIView *footLine;

/** 奔到文本的宽度 */
@property (nonatomic,assign) CGFloat titleWeight;

@property (nonatomic,assign) CGFloat lineX;

@property (nonatomic,strong) NSMutableArray *channelLabels;

@end

@implementation XBCommunityScrollView

+ (instancetype)ChannelWithLabels:(NSArray *)labels andFrame:(CGRect)frame
{
    // 创建
    XBCommunityScrollView *scrollView = [[self alloc] init];
    
    // 赋值
    scrollView.labels = labels;
    scrollView.frame = frame;
    
    // 设置
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    
    // 安装label
    [scrollView setupLabels];
    // 安装footLine
    [scrollView setupLineView];
    
    return scrollView;
}

- (void)setupLineView
{
    // 创建并添加
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(self.lineX, self.bounds.size.height - 6, self.titleWeight, 3)];
    self.footLine = lineView;
    [self addSubview:lineView];
    // 颜色
    lineView.backgroundColor = [UIColor orangeColor];
}

- (void)setupLabels
{
    // 间隔
    CGFloat marginX = margin;

    CGFloat lblH = self.frame.size.height;
    NSInteger i = 0;
    // 遍历数据，取出标题名称
    for (NSString *labelTitle in self.labels) {

        // 创建Label控件
        XBCommunityLabel *currentLabel = [[XBCommunityLabel alloc] initWithTitle:labelTitle];
        
        [self.channelLabels addObject:currentLabel];
        // 获取自适应宽度
        CGFloat lblW = currentLabel.frame.size.width;
        self.titleWeight = lblW;
        
        // X值
        CGFloat lblX = (ScreenW - ((self.labels.count * lblW)+ (self.labels.count-1)*marginX)) *0.5;
        self.lineX = lblX;
        
        // 赋值frame
        currentLabel.frame = CGRectMake(lblX+i*(lblW + marginX), 0, lblW, lblH);
        
        // 添加到自身
        [self addSubview:currentLabel];
        i++;
#pragma mark - 标题点击事件
        __weak typeof(currentLabel) weakCurrentLbl = currentLabel;
        // 设置标题点击事件
        [currentLabel setSelectedlabel:^{
            
            // 重新设置currentIndex
            self.currentIndex = [self.subviews indexOfObject:weakCurrentLbl];

            // 发送通知
            [[NSNotificationCenter defaultCenter] postNotificationName:ChannelTitleCurrenIndexChanged object:nil userInfo:@{@"index":@(self.currentIndex)}];
        }];
        // 设置滚动范围
        self.contentSize = CGSizeMake(lblX, 0);
    }

    // 默认第一个选中
    self.currentIndex = 0;
    XBCommunityLabel *firstLbl = self.subviews[0];
    firstLbl.textColor = [UIColor orangeColor];
    firstLbl.selectedlabel();
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}

#pragma mark - ======== dealloc ========
- (void)dealloc
{
    NSLog(@"%s",__func__);
}
- (NSMutableArray *)channelLabels
{
    if (!_channelLabels) {
        _channelLabels = [NSMutableArray array];
    }
    return _channelLabels;
}

NSString *const ChannelTitleCurrenIndexChanged = @"titleCurrentIndexChanged";
@end
